package cn.panda.edu.controller;

import cn.panda.edu.model.Exam;
import cn.panda.edu.service.ExamService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.ResultDataUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 
 * 类描述：考试信息的控制层
 * 
 * @author: 陈飞帆 @date： 2018-01-17 上午11:11:35
 */
@RestController
@RequestMapping("/api/teacher/exam")
public class TeacherExamController {
	final int PAGESIZE = 10;
	@Autowired
	private ExamService examService;

	@ApiOperation(value = "分页获取考试信息列表", notes = "") // 用以指明API名称
	@ApiImplicitParam(name = "pageIndex", value = "当前页数", required = true, dataType = "Integer")
	@RequestMapping(value = "/getlist", method = RequestMethod.POST)
	public Object getAllExam(Integer pageIndex) {
		if(pageIndex==null) {
			pageIndex=1;
		}

		PageInfo<Exam> pageInfo = examService.getAllExam(pageIndex, Constant.PAGE_SIZE);
		return ResultDataUtil.resData(200, "", pageInfo);
	}

	@ApiOperation(value = "根据id删除考试信息", notes = "")
	@ApiImplicitParam(name = "id", value = "考试信息id", required = true, dataType = "Integer")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public  Object deleteByPrimaryKey(Integer id) {
		int status=200;
		String message=null;
		try {
			examService.deleteByPrimaryKey(id);
			status=200;
			message="删除成功！";
		} catch (Exception e) {
			status=500;
			message="删除失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}

	@ApiOperation(value = "发布考试信息", notes = "")
	//@ApiImplicitParam(name = "exam", value = "考试信息实体类", required = true, dataType = "exam")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public Map<String, Object> insert(Exam exam) {
		Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
		try {
			examService.insertSelective(exam);
			resultMap.put("status", 200);
			resultMap.put("message", "添加成功！");
		} catch (Exception e) {
			resultMap.put("status", 500);
			resultMap.put("message", "添加失败!");
		}
		return resultMap;
	}
}
